const getMysql = require('../mysql')


// 首页分类以及分页
const destCardPage = async(req, res) => {
    // console.log(req.body.address);
    // let address = req.body.address;
    let pno = Number((req.body.mid - 1) * 12)
    let sql = 'select * from dest_home_card limit ?,12'
    let sql2 = `select count(*) as num from dest_home_card`
    getMysql(sql,pno).then(result => {
        getMysql(sql2).then(count => {
            res.send({
                num:count,
                pages:Math.ceil(Number(count[0].num)/12),
                code: 200,
                message: '查询成功！！',
                data: result
            })
        })

    }).catch(err => {
        res.send({
            code: 201,
            message: '查询失败'
        })
    })
}

// 按时间筛选
const destCardSelect = async(req, res) => {
    // console.log(req.body.month);
    let month = req.body.way;
    let pno = Number((req.body.mid - 1) * 12)
    let sql = 'select * from dest_home_card where month = ?  limit ?,12'
    let sql2 = `select count(*) as num from dest_home_card where month = ? `
    getMysql(sql,month,pno).then(result => {
        getMysql(sql2,month).then(count => {
            res.send({
                num:count,
                pages:Math.ceil(Number(count[0].num)/12),
                code: 200,
                message: '查询成功！！',
                data: result
            })
        })

    }).catch(err => {
        res.send({
            code: 201,
            message: '查询失败'
        })
    })
}

// 按主题筛选
const destCardSelectZT = async(req, res) => {
    // console.log(req.body.theme);
    let theme = req.body.way;
    let pno = Number((req.body.mid - 1) * 12)
    let sql = 'select * from dest_home_card where theme = ?  limit ?,12'
    let sql2 = `select count(*) as num from dest_home_card where theme = ? `
    getMysql(sql,theme,pno).then(result => {
        getMysql(sql2,theme).then(count => {
            res.send({
                num:count,
                pages:Math.ceil(Number(count[0].num)/12),
                code: 200,
                message: '查询成功！！',
                data: result
            })
        })

    }).catch(err => {
        res.send({
            code: 201,
            message: '查询失败'
        })
    })
}


// 按季节筛选
const destCardSeason = async(req, res) => {
    // console.log(req.body.season);
    let season = req.body.way;
    let pno = Number((req.body.mid - 1) * 12)
    let sql = 'select * from dest_home_card where season = ?  limit ?,12'
    let sql2 = `select count(*) as num from dest_home_card where season = ? `
    getMysql(sql,season,pno).then(result => {
        getMysql(sql2,season).then(count => {
            res.send({
                num:count,
                pages:Math.ceil(Number(count[0].num)/12),
                code: 200,
                message: '查询成功！！',
                data: result
            })
        })

    }).catch(err => {
        res.send({
            code: 201,
            message: '查询失败'
        })
    })
}


// 按出行方式筛选
const destCardMode = async(req, res) => {
    // console.log(req.body.mode);
    let mode = req.body.way;
    let pno = Number((req.body.mid - 1) * 12)
    let sql = 'select * from dest_home_card where mode = ?  limit ?,12'
    let sql2 = `select count(*) as num from dest_home_card where mode = ? `
    getMysql(sql,mode,pno).then(result => {
        getMysql(sql2,mode).then(count => {
            res.send({
                num:count,
                pages:Math.ceil(Number(count[0].num)/12),
                code: 200,
                message: '查询成功！！',
                data: result
            })
        })

    }).catch(err => {
        res.send({
            code: 201,
            message: '查询失败'
        })
    })
}



// 目的地模糊查询
const destSearch = async (req, res) => {
    let title = "%" + req.body.title + "%"
    console.log(title);
    let sql = `select * from dest_home_card where title like ?`
    getMysql(sql, title).then(result => {
        res.send({
            code: 200,
            message: '查询成功！！',
            data: result
        })
    }).catch(err => {
        console.log(err);
        res.send({
            code: 201,
            message: '查询失败！！',
        })
    })
}

// 目的地详情页查询关键字
const destSearchDetail = async (req, res) => {
    let id = req.body.id
    let sql = `select * from dest_home_card where id= ?`
    getMysql(sql, id).then(result => {
        res.send({
            code: 200,
            message: '查询成功！！',
            data: result
        })
    }).catch(err => {
        console.log(err);
        res.send({
            code: 201,
            message: '查询失败！！',
        })
    })
}


module.exports = {
    destCardPage,
    destCardSelect,
    destCardSelectZT,
    destCardSeason,
    destCardMode,
    destSearch,
    destSearchDetail
}